package com.liveinstars.im.corebean;

import com.liveinstars.im.domain.dao.model.ChatGroup;
import com.liveinstars.im.domain.dao.model.Todo;
import com.liveinstars.im.entity.ChatGroupP1;
import com.liveinstars.im.entity.ChatMessage;
import com.liveinstars.im.entity.Msg;
import com.liveinstars.im.entity.User;
import com.liveinstars.api.im.vo.UserVO;
import org.apache.commons.lang.StringUtils;

import java.util.List;

public class MsgHelper {
	/**
	 * 生成一个带返回消息的消息
	 * 
	 * @param type
	 * @param message
	 * @return
	 */
	public static Msg.Message newResultMessage(Msg.MessageType type, String message) {
		Msg.ResultMessage.Builder builder = Msg.ResultMessage.newBuilder();
		Msg.ResultMessage rtMessage = builder.setMessage(message)
				.setMessageType(type).build();
		Msg.Message.Builder b = Msg.Message.newBuilder();
		Msg.Message m = b.setResultMessage(rtMessage).setMessageType(type)
				.build();
		return m;
	}

	/**
	 * 生成一个带ping/pong消息的消息
	 * 
	 * @param type
	 * @param clientId
	 * @return
	 */
	public static Msg.Message newPingMessage(Msg.MessageType type, String clientId) {
		Msg.PingMessage.Builder bu = Msg.PingMessage.newBuilder();
		Msg.PingMessage rtMessage = bu.setClientId(clientId)
				.setMessageType(type).build();
		Msg.Message.Builder b = Msg.Message.newBuilder();
		Msg.Message m = b.setPingMessage(rtMessage).setMessageType(type)
				.build();
		return m;
	}


	/**
	 * 用json包装比较难处理的数据
	 * 
	 * @param type
	 * @param jsonStr
	 * @return
	 */
	public static Msg.Message newJSonMessage(int type, String jsonStr) {
		Msg.JsonMessage jsonMessage = Msg.JsonMessage.newBuilder()
				.setJsonMessageType(type).setJsonStr(jsonStr).build();

		Msg.Message.Builder b = Msg.Message.newBuilder();
		Msg.Message m = b.setJsonMessage(jsonMessage)
				.setMessageType(Msg.MessageType.JSON_DATA).build();
		return m;
	}

	/**
	 * 聊天消息
	 * 
	 * @param fromId
	 * @param toId
	 * @param content
	 * @param token
	 * @param date
	 * @param contentType
	 * @return
	 */
	public static Msg.Message newChatMessage(int fromId, int toId, String content,
                                             String token, boolean transfer, Integer date, int id,
                                             int contentType, String fileGroupName, String path,
                                             int chatGroupId, int discussionGroupId, int msgType, String uuid,
                                             int status) {
		Msg.ChatMessage chatMessage = Msg.ChatMessage.newBuilder()
				.setContent(content).setFromId(fromId).setToId(toId)
				.setMsgType(ChatMessage.MSG_TYPE_UU).setToken(token)
				.setChatMessageId(id).setDate(date).setTransfer(transfer)
				.setChatGroupId(chatGroupId).setUuid(uuid).setStatus(status)
				.setMsgType(msgType)
				.setContentType(contentType).build();

		Msg.Message.Builder b = Msg.Message.newBuilder();
		Msg.Message m = b.setChatMessage(chatMessage)
				.setMessageType(Msg.MessageType.CHAT_MESSAGE).build();
		return m;
	}

	/**
	 * 需要注意string和integer设置proto有null的情况，要有默认值，否则抛出空指针
	 * @param ms
	 * @return
	 */
	public static Msg.Message newChatMessage(ChatMessage ms) {

		Msg.ChatMessage chatMessage = Msg.ChatMessage.newBuilder()
				.setContent(ms.getContent()).setFromId(ms.getFromId())
				.setToId(ms.getToId()).setMsgType(ms.getMsgType())
				.setDate(ms.getDate())
				.setChatMessageId(ms.getId())
				.setChatGroupId(ms.getChatGroupId())
				.setMsgType(ms.getMsgType())
				.setUuid(ms.getUuid())
				.setStatus(ms.getStatus())
				.setFileName(ms.getFileName())
				.setFilePath(ms.getPath())
				.setFileType(ms.getFileType())
				.setUsername(ms.getUserName())
				.setUseravator(ms.getUserAvator())
				.setFileParam(ms.getFileParam())
				.setUserGroupId(ms.getUserGroupId())
				.setContentType(ms.getContentType()).setTransfer(false).build();
		Msg.Message.Builder b = Msg.Message.newBuilder();
		Msg.Message m = b.setChatMessage(chatMessage)
				.setMessageType(Msg.MessageType.CHAT_MESSAGE).build();
		return m;
	}

	/**
	 * 获取好友信息,online属性目前没有应用，可以从redis中查询
	 *
	 * @param friends
	 * @return
	 */
	public static Msg.Message newFriendsListMessage(List<UserVO> friends) {
		Msg.Message.Builder b = Msg.Message.newBuilder();
		if (friends != null) {
			int index = 0;
			for (UserVO f : friends) {
				Msg.Friends.Builder fb = Msg.Friends.newBuilder();
				if (f.getLogo() != null) {
					fb.setAvatarPath(f.getLogo());
				} else {
					fb.setAvatarPath("");
				}
				fb.setId(f.getId());
				fb.setName(f.getName());
				if (StringUtils.isNotEmpty(f.getRemarkName())) {
					fb.setRemarkName(f.getRemarkName());
				} else {
					fb.setRemarkName("");
				}
				fb.setSignature(f.getIndividualResume());
				fb.setOnline(true);
				fb.setOnlineType(0);
				fb.setUserId(f.getId());
				fb.setGroupId(f.getGroupId());
				fb.setGroupName(ChatGroup.UserLevel.getUserLevel(f.getGroupId()).getValue());
				fb.setDate(f.getDate());
				b.addFriendsListMessage(index, fb.build());
				index++;
			}
		}

		b.setMessageType(Msg.MessageType.FRIENDS_LIST);
		Msg.Message m = b.build();
		return m;
	}

	public static Msg.Message newChatMessageListMessage(List<ChatMessage> messages) {
		Msg.Message.Builder b = Msg.Message.newBuilder();
		if (messages != null) {

			for (ChatMessage ms : messages) {
				Msg.ChatMessage chatMessage = Msg.ChatMessage.newBuilder()
						.setContent(ms.getContent()).setFromId(ms.getFromId())
						.setToId(ms.getToId()).setMsgType(ms.getMsgType())
						.setDate(ms.getDate())
						.setChatMessageId(ms.getId()).setToken(ms.getToken())
						.setFileName(ms.getFileName())
						.setFilePath(ms.getPath())
						.setFileType(ms.getFileType())
						.setUsername(ms.getUserName())
						.setUseravator(ms.getUserAvator())
						.setContentType(ms.getContentType())
						.setTransfer(false)
						.setUserGroupId(ms.getUserGroupId())
						.setChatGroupId(ms.getChatGroupId())
						.setFileParam(ms.getFileParam())
						.setUuid(ms.getUuid()).setStatus(ms.getStatus())
						.setMsgType(ms.getMsgType()).build();
				b.addChatMessageListMessage(chatMessage);
			}
		}
		b.setMessageType(Msg.MessageType.CHAT_MESSAGE_LIST);
		Msg.Message m = b.build();
		return m;
	}

	public static Msg.Message newChatMessageHistoryListMessage(List<ChatMessage> messages) {
		Msg.Message.Builder b = Msg.Message.newBuilder();
		if (messages != null) {
			for (ChatMessage ms : messages) {
				Msg.ChatMessage chatMessage = Msg.ChatMessage.newBuilder()
						.setContent(ms.getContent()).setFromId(ms.getFromId())
						.setToId(ms.getToId()).setMsgType(ms.getMsgType())
						.setDate(ms.getDate())
						.setChatMessageId(ms.getId()).setToken(ms.getToken())
						.setFileName(ms.getFileName())
						.setFilePath(ms.getPath())
						.setFileType(ms.getFileType())
						.setContentType(ms.getContentType()).setTransfer(false)
						.setChatGroupId(ms.getChatGroupId())
						.setUserGroupId(ms.getUserGroupId())
						.setUuid(ms.getUuid()).setStatus(ms.getStatus())
						.setUsername(ms.getUserName())
						.setUseravator(ms.getUserAvator())
						.setFileParam(ms.getFileParam())
						.setMsgType(ms.getMsgType()).build();
				b.addChatMessageListMessage(chatMessage);
			}
		}
		b.setMessageType(Msg.MessageType.CHAT_MESSAGE_LIST_HISTORY);
		Msg.Message m = b.build();
		return m;
	}


	public static Msg.Message newTodoListMessage(List<Todo> messages) {
		Msg.Message.Builder b = Msg.Message.newBuilder();
		if (messages != null) {

			for (Todo ms : messages) {

				Msg.TodoMessage todoMessage = Msg.TodoMessage.newBuilder()
						.setTodoId(ms.getId()).setTodoSubject(ms.getSubject())
						.setType(ms.getType()).setGroupId(ms.getGroupId())
						.setFromId(ms.getFromId()).setToId(ms.getUserId())
						.setRequestMsg(ms.getRequestMsg()).build();
				b.addTodoListMessage(todoMessage);
			}
		}
		b.setMessageType(Msg.MessageType.TODO_MESSAGE_LIST);
		Msg.Message m = b.build();
		return m;
	}


	/**
	 * 聊天群
	 * 
	 * @param chatGroup
	 * @return
	 */
	public static Msg.Message newChatGroupListMessage2(List<ChatGroupP1> chatGroup) {
		Msg.Message.Builder b = Msg.Message.newBuilder();
		if (chatGroup != null) {
			int index = 0;
			for (ChatGroupP1 fg : chatGroup) {
				Msg.ChatGroup.Builder gb = Msg.ChatGroup.newBuilder();

				gb.setId(fg.getId());
				gb.setName(fg.getName() + "");
				gb.setSlogan(fg.getSlogan() + "");
				gb.setGroupLogo(fg.getGroupLogo());
				gb.setType(fg.getBigClassId());
				gb.setAuthRule(fg.getAuthRule() + "");
				gb.setGroupLevel(fg.getGroupLevel() + "");
				gb.setQrcode(fg.getQrcode() + "");
				gb.setUserLevel(fg.getUserLevel() + "");
				gb.setGroupCount(fg.getUserCount());
				gb.setCreateById(fg.getCreateById());
				gb.setNoDisturbing(fg.isNoDisturbing());
				gb.setIdentification(fg.getIdentification());
				Msg.ChatGroup g = gb.build();
				b.addChatGroupListMessage(index, g);
				index++;
			}
		}
		b.setMessageType(Msg.MessageType.CHAT_GROUP_LIST);
		Msg.Message m = b.build();
		return m;
	}

	/**
	 * 聊天群
	 *
	 * @param chatGroup
	 * @return
	 */
	public static Msg.Message newChatGroupListMessage(List<ChatGroup> chatGroup) {
		Msg.Message.Builder b = Msg.Message.newBuilder();
		if (chatGroup != null) {
			int index = 0;
			for (ChatGroup fg : chatGroup) {
				Msg.ChatGroup.Builder gb = Msg.ChatGroup.newBuilder();

				gb.setId(fg.getId());
				gb.setName(fg.getName());
				gb.setSlogan(fg.getSlogan() + "");
				gb.setGroupLogo(fg.getGroupLogo());
				gb.setType(fg.getBigClassId());
				gb.setAuthRule(fg.getAuthRule() + "");
				gb.setGroupLevel(fg.getGroupLevel() + "");
				gb.setQrcode(fg.getQrcode());
				gb.setUserLevel(fg.getUserLevel() + "");
				Msg.ChatGroup g = gb.build();
				b.addChatGroupListMessage(index, g);
				index++;
			}
		}
		b.setMessageType(Msg.MessageType.CHAT_GROUP_LIST);
		Msg.Message m = b.build();
		return m;
	}


	public static Msg.Message newUserListMessage(List<User> users) {
		Msg.Message.Builder b = Msg.Message.newBuilder();
//		if (users != null) {
//			int index = 0;
//			for (User u : users) {
//				Msg.User.Builder gb = Msg.User.newBuilder();
//				gb.setId(u.getId());
//				gb.setName(u.getName());
////				gb.setAccount(u.getAccount());
////				if (u.getAvatar() != null) {
////					gb.setAvatarPath(u.getAvatar().getGroupName() + "#"
////							+ u.getAvatar().getPath());
////				} else {
////					gb.setAvatarPath("");
////				}
////				gb.setBirthday(StringUtils.getDateString(u.getBirthday()));
////				gb.setGender(u.getGender());
////				gb.setSignature(u.getSignature() + "");
//				Msg.User g = gb.build();
//				b.addUserListMessage(index, g);
//				index++;
//			}
//		}
//		b.setMessageType(Msg.MessageType.USER_LIST);
		Msg.Message m = b.build();
		return m;
	}

	public static Msg.Message newReceiptMessage(String uuid, int status) {

		return newReceiptMessage(uuid, status, "");
	}

    public static Msg.Message newReceiptMessage(String uuid, int status, String message) {
        Msg.Message.Builder b = Msg.Message.newBuilder();

        Msg.ReceiptMessage.Builder rb = Msg.ReceiptMessage.newBuilder();
        rb.setStatus(status).setUuid(uuid).setMessage(message);

        Msg.ReceiptMessage rm = rb.build();

        b.setMessageType(Msg.MessageType.RECEIPT);
        b.setReceiptMessage(rm);
        Msg.Message m = b.build();
        return m;
    }

	public static Msg.Message newTodoMessage(Todo todo) {
		Msg.TodoMessage todoMessage = Msg.TodoMessage.newBuilder()
				.setTodoId(todo.getId()).setTodoSubject(todo.getSubject())
				.setType(todo.getType()).setRequestMsg(todo.getRequestMsg())
				.setFromId(todo.getFromId()).setToId(todo.getUserId())
				.setGroupId(todo.getGroupId())
				.build();

		Msg.Message.Builder b = Msg.Message.newBuilder();
		Msg.Message m = b.setTodoMessage(todoMessage)
				.setMessageType(Msg.MessageType.TODO).build();
		return m;
	}

	public static Msg.Message newNoticeMessage(int noticeType, int showType, String data) {
		Msg.NoticeMessage noticeMessage = Msg.NoticeMessage.newBuilder()
				.setNoticeType(noticeType)
				.setShowType(showType)
				.setData(data)
				.build();
		Msg.Message.Builder builder = Msg.Message.newBuilder();
		Msg.Message msg = builder.setNoticeMessage(noticeMessage)
				.setMessageType(Msg.MessageType.NOTICE_MESSAGE).build();
		return msg;
	}

	public static Msg.Message newPassThroughMessage(Long toId, int type, Msg.Message message) {
		Msg.PassThroughMessage passThroughMessage = Msg.PassThroughMessage.newBuilder()
				.setMessage(message)
				.setToId(toId)
				.setType(type)
				.build();
		Msg.Message.Builder builder = Msg.Message.newBuilder();
		Msg.Message msg = builder.setPassThroughMessage(passThroughMessage)
				.setMessageType(Msg.MessageType.PASS_THROUGH_MESSAGE).build();
		return msg;
	}
}
